﻿///////////////////////////////////////////////////////////////////////////////
// Copyright 2016 lingzimaike@gmail.com
// GPLv2
// Author: lingzimaike
///////////////////////////////////////////////////////////////////////////////

#ifndef PAPA_THREAD_MUTEX_H_
#define PAPA_THREAD_MUTEX_H_

#include <papa/base.h>
#include <papa/platform.h>
#include <papa/thread/lock_guard.h>

namespace papa {
namespace thread {

	class Mutex
	{
	public:
		Mutex(void);
		~Mutex(void);

		inline void Lock(void);
		inline void Unlock(void);

	private:
		platform::__mutex mutex_;

		PAPA_DISALLOW_COPY_AND_ASSIGN(Mutex)
	};

#if defined(__WINDOWS__)
    inline Mutex::Mutex(void)
    {
        ::InitializeCriticalSection(&mutex_);
    }

    inline Mutex::~Mutex(void)
    {
        ::DeleteCriticalSection(&mutex_);
    }

    inline void Mutex::Lock(void)
    {
        ::EnterCriticalSection(&mutex_);
    }

    inline void Mutex::Unlock(void)
    {
        ::LeaveCriticalSection(&mutex_);
    }
#else
#endif

}}  // namespace papa::thread

#define PAPA_LOCK_MUTEX(m)	papa::thread::LockGuard<papa::thread::Mutex> guard(m)

#endif // PAPA_THREAD_MUTEX_H_
